<!--
 * @version:
 * @Company:
 * @Description:
 * @FilePath: \src\views\tool\gen\modal\preview\index.vue
-->
<template>
  <BasicModal
    v-bind="$attrs"
    destroyOnClose
    :useWrapper="false"
    :canFullscreen="false"
    :bodyStyle="{ height: '70vh' }"
    :showOkBtn="false"
    cancelText="关闭"
    @closeFunc="closeFunc"
    @ok="handleClickOk"
    @cancel="handleClickOk"
    @register="register"
    title="代码预览"
    width="1000px"
  >
    <div class="previewCode">
      <a-select v-model:value="select.value" :size="size" style="width: 200px" :options="select.options" @change="handleChange"></a-select>
      <Tabs v-model:activeKey="activeKey" tabPosition="top" animated>
        <template #rightExtra>
          <a-button type="primary" size="small" @click="handleClickCopy"> 复制 </a-button>
        </template>
        <TabPane v-for="key in previewCode.data.keys()" :key="key" :tab="key">
          <div class="tab-info">
            <highlightjs autodetect :code="previewCode.data.get(key)" :id="key" />
          </div>
        </TabPane>
      </Tabs>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { Tabs } from 'ant-design-vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import * as _API from '../../api';
  import { PreviewParams, PreviewResultModel } from '../../api/model';
  import { useMessage } from '/@/hooks/web/useMessage';

  export default defineComponent({
    components: {
      useMessage,
      BasicModal,
      Tabs,
      TabPane: Tabs.TabPane,
    },
    setup() {
      let activeKey = ref('');
      let loading = ref(true);
      let tableId: string | number = '';
      const select = ref({
        value: '' as string | number,
        options: [] as any,
      });
      const previewCode: PreviewResultModel = { data: new Map<string, string>() };
      const [register, { closeModal }] = useModalInner(async (data: PreviewParams) => {
        tableId = data.tableId;
        select.value.options = data.tplList;
        select.value.value = data.tplList[0]?.value;
        await handlePreview({ tableId: data.tableId, tplList: [data.tplList[0]?.value] });
      });

      const handlePreview = async ({ tableId, tplList }) => {
        await _API.previewAPI({ tableId: tableId, tplList: tplList }).then((res) => {
          Object.keys(res.data).forEach((value: string, index: number) => {
            if (index == 0) {
              activeKey.value = value;
            }
            previewCode.data.set(value, res.data[value]);
          });
          loading.value = false;
        });
      };

      function handleClickOk() {
        previewCode.data.clear();
        activeKey.value = '';
        closeModal();
      }

      function closeFunc() {
        previewCode.data.clear();
        activeKey.value = '';
        return true;
      }

      const { createMessage } = useMessage();

      const handleClickCopy = () => {
        const element = document.getElementById(activeKey.value);
        const selection = window.getSelection();
        if (element && selection) {
          const range = document.createRange();
          range.selectNodeContents(element);
          selection.removeAllRanges();
          selection.addRange(range);
          navigator.clipboard.writeText(selection.focusNode?.textContent || '');
          createMessage.success('复制成功');
        } else {
          createMessage.error('操作失败');
        }
      };
      const handleChange = (event) => {
        previewCode.data.clear();
        activeKey.value = '';
        handlePreview({ tableId: tableId, tplList: [event] });
      };
      return {
        loading,
        activeKey,
        previewCode,
        select,
        register,
        handleClickOk,
        closeFunc,
        handleClickCopy,
        handleChange,
      };
    },
  });
</script>
<style lang="less">
  .previewCode {
    .tab-info {
      height: 60vh;
      overflow-y: auto;
    }
  }
</style>
